package mdt.figure;

import org.tigris.gef.presentation.FigRect;
import mdt.model.IMDTComment;
import mdt.model.INodeModel;

import java.awt.*;

/**
 * Created by xiaobyu on 6/17/2016.
 */
public class FigRichText extends FigNodeEditable{
    private static final long serialVersionUID = 1L;
		private FigRect boundryFig;

    public FigRichText(IMDTComment owner) {
        super(owner, TextType.MULTI_LINE);

        boundryFig = new FigRect(0, 0, figText.getWidth(), figText.getHeight());
        boundryFig.setVisible(false);

        addFig(boundryFig);
        setOwner(owner);
    }

    public void setOwner(Object owner) {
        super.setOwner(owner);
        if (boundryFig != null) {
            this.bindPort(owner, boundryFig);
        }
    }

    public boolean isDragConnectable() {
        return false;
    }

    protected void setBoundsImpl(int x, int y, int w, int h) {
        super.setBoundsImpl(x, y, w, h);
        boundryFig.setBounds(x, y, w, h);
    }

    protected void initTextFig(INodeModel model) {
        super.initTextFig(model);
        figText.setLineWidth(1);
        figText.setTopMargin(2);
        figText.setBotMargin(2);
        figText.setLeftMargin(2);
        figText.setRightMargin(2);
        figText.setFillColor(Color.white);
        //figText.setEnableArc(true);
    }

    @Override
    public void renderingChanged() {
        updateBounds();
        damage();
    }

    public void updateBounds() {
        Rectangle bbox = getBounds();
        Dimension minSize = getMinimumSize();
        bbox.width = Math.max(bbox.width, minSize.width);
        bbox.height = Math.max(bbox.height, minSize.height);
        setBounds(bbox.x, bbox.y, bbox.width, bbox.height);
    }
}
